package com.htx.oldbookpavilion.service.mycenter.impl;

import com.baidu.fsg.uid.UidGenerator;
import com.htx.oldbookpavilion.entity.*;
import com.htx.oldbookpavilion.mapper.BookInfoExtMapper;
import com.htx.oldbookpavilion.mapper.BookInfoMapper;
import com.htx.oldbookpavilion.mapper.BookTypeDictMapper;
import com.htx.oldbookpavilion.mapper.UserInfoMapper;
import com.htx.oldbookpavilion.service.authorityservice.UserInfoService;
import com.htx.oldbookpavilion.service.bookinfoservice.BookInfoService;
import com.htx.oldbookpavilion.service.mycenter.MyCenterService;
import com.htx.oldbookpavilion.common.StandardPageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class MyCenterServiceImpl implements MyCenterService {

    private Logger logger = LoggerFactory.getLogger(MyCenterServiceImpl.class);

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Autowired
    private BookInfoExtMapper bookInfoExtMapper;

    @Autowired
    private BookTypeDictMapper bookTypeDictMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UidGenerator uidGenerator;

    /**
     * 上传书籍
     * @param bookInfo
     * @return
     */
    @Override
    public BookInfo uploadBook(BookInfo bookInfo) {
        //生成随机id
        long bookInfoId = uidGenerator.getUID();
        bookInfo.setBookId(bookInfoId);
        bookInfo.setCreateTime(new Date());
        int insert = bookInfoMapper.insert(bookInfo);
        logger.info(bookInfo.getBookId() + "--" + bookInfo.getUserInfoId() + "--" + bookInfo.getBookName());
        if (1 == insert) {
            return bookInfo;
        }
        return null;
    }

    /**
     * 更新书籍信息
     * @param bookInfo
     * @return
     */
    @Override
    public BookInfo updateBook(BookInfo bookInfo) {
        int count = bookInfoExtMapper.updateByPrimaryKey(bookInfo);
        if(count > 0){
            return bookInfo;
        }
        return null;
    }

    /**
     * 获取已上传书籍
     * @param sPageData
     * @return
     */
    @Override
    public StandardPageData<BookInfo> getUploadBooks(StandardPageData<BookInfo> sPageData) {
        return bookInfoService.getBooksByPage(sPageData);
    }

    /**
     * 删除书籍
     * @param bookInfoList
     * @return
     */
    @Override
    public int deleteUploadBooks(List<BookInfo> bookInfoList) {
        int count = 0;
        if(bookInfoList.size() == 1){
            //删除单条记录
            //count = bookInfoCustomeMapper.deleteByPrimaryKey(bookInfoList.get(0).getBookId());
            bookInfoList.get(0).setIsDeleted("Y");
            count = bookInfoExtMapper.updateByPrimaryKey(bookInfoList.get(0));
        }else{
            //批量删除多条记录
            BookInfoExample bookInfoExample = new BookInfoExample();
            BookInfo deletedBook = new BookInfo();
            deletedBook.setIsDeleted("Y");
            for (BookInfo bookInfo : bookInfoList) {
                bookInfoExample.or().andBookIdEqualTo(bookInfo.getBookId());
            }
            //count = bookInfoCustomeMapper.deleteByExample(bookInfoExample);
            count = bookInfoExtMapper.updateByExampleSelective(deletedBook, bookInfoExample);
        }
        return count;
    }

    /**
     * 获取书籍标签信息
     * @return
     */
    @Override
    public List<BookTypeDict> getBookTypeData() {
        List<BookTypeDict> bookTypeDicts = bookTypeDictMapper.selectByExample(new BookTypeDictExample());
        return bookTypeDicts;
    }

    /**
     * 校验密码是否正确
     * @param userInfo
     * @return
     */
    @Override
    public boolean validatePassword(UserInfo userInfo) {
        UserInfoExample userInfoExample = new UserInfoExample();
        if(userInfo.getUserName() == null){
            logger.info("用户名为空，返回false");
            return false;
        } else if (userInfo.getPassword() == null) {
            logger.info("密码为空，返回false");
            return false;
        }
        //解析密码
        String rawPassword = userInfo.getPassword();
        StringBuffer newPassword = new StringBuffer();
        for (int i = 0 ; i < rawPassword.length() ; i ++){
            char passChar = (char)(rawPassword.charAt(i) - 1);
            newPassword.append(passChar);
        }
        userInfoExample.or().andUserNameEqualTo(userInfo.getUserName())
                .andPasswordEqualTo(newPassword.toString());
        List<UserInfo> userInfos = userInfoMapper.selectByExample(userInfoExample);
        if(userInfos != null && userInfos.size() > 0){
            return true;
        }
        return false;
    }

    /**
     * 根据id获取用户信息
     * @param ids
     * @return
     */
    @Override
    public List<UserInfo> getUserInfoByIds(String[] ids) {
        List<UserInfo> userInfoList = userInfoService.getUserInfoByIds(ids);
        for (UserInfo userInfo : userInfoList) {
            userInfo.setPassword(null);
            userInfo.setUserRealName(null);
            userInfo.setIdCard(null);
            userInfo.setAuthority(null);
        }
        return userInfoList;
    }
}
